<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Route;
use App\Admin\ShareDir;
use Storage;
use DB;

class ShareDirController extends Controller
{
    private $dir = 'shareDir';
    private $storage = null;
    private $totalCap = '50 M';
    private $table = 'share_dir';
    public function __construct(){
        $this->storage = Storage::disk('public');

    }

    //打印预览
    public function preview(){
        $id = request('id');
        $info = DB::table('share_dir')->where('id',$id)->first();
        // dd($info);
        $file = $info->path;
        if (!$this->storage->exists($file)) {
            return '文件不存在';
        }
        return response()->file($this->storage->path($file));
        // return $this->storage->response($file, null,[],'inline');
        // return $this->storage->response($file, $info->filename);
    }

    //权限列表展示
    public function index(Request $request)
    {
        $dir = $this->dir;
        $storage = $this->storage;
        $data['dir'] = $dir;
        $data['dirCap'] = $this->totalCap;
        $usedCap = DB::table($this->table)->sum('file_size');
        $data['dirLeftCap'] = (intval($this->totalCap)-ceil($usedCap/1024/1024 * 100) / 100).' M';
        $data['files'] = DB::table($this->table)->orderBy('id','desc')->get();
        $data['stds'] = DB::table('a_standard')->pluck('num');
        // dd($data);
        return view('admin.shareDir.index', compact('data'));
    }

    //添加
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $storage = $this->storage;
            $dir = $this->dir;
            $file = $request->file('file');
            $in = [];
            $in['file_size'] = $fileSize = $file->getSize();
            $usedCap = DB::table($this->table)->sum('file_size');
            $usedCap += $fileSize;
            if($usedCap > intval($this->totalCap)*1024*1024 ){
                return '容量不够用了！请先清除无用文件！';
            }
            $in['std_num'] = request('std_num');
            $in['filename'] = $filename = $file->getClientOriginalName();
            $in['ext'] = $file->getClientOriginalExtension();
            if(DB::table($this->table)->where('filename',$filename)->exists()){
                return '文件已存在！';
            }
            $in['path'] = $path = $storage->putFileAs($dir, $file, $filename);
            $res = $path ? DB::table($this->table)->insert($in) : 0;
            return $res ? 1 : 0;
        }
    }

    //权限删除
    public function del(Request $request)
    {
        $id = request('id');
        $info = DB::table($this->table)->where('id',$id)->first();
        $res = DB::table($this->table)->delete($id);
        $storage = $this->storage;
        $storage->delete($info->path);
        return $res ? 1 : 0;
    }

    //修改
    public function edit(){
        $params = request()->only('name','value','id');
        $storage = $this->storage;
        $table = $this->table;
        try{
            $info = DB::table($table)->where('id',$params['id'])->first();
            if ($params['name'] == 'filename') {
                $oldPath = $info->path;
                $newPath = str_replace("/{$info->filename}","/{$params['value']}",$oldPath);
                if (!$storage->exists($oldPath)) {
                    return '文件不存在！';
                }
                $storage->move($oldPath,$newPath);
                $res = DB::table($table)->where('id',$params['id'])->update(['filename'=>$params['value'],'path'=>$newPath]);
            }else{
                $res = DB::table($table)->where('id',$params['id'])->update([$params['name']=>$params['value']]);
            }
        }catch(\Exception $e){
            return 0;
        }
        return $res ? 1 : 0;;
    }

     //同步已有文件
    public function sync(){
        $storage = $this->storage;
        $dir = $this->dir;
        $files = $storage->files($dir);
        $count = 0;
        foreach($files as $file){
            if(DB::table($this->table)->where('path',$file)->doesntExist()){
                $size = $storage->size($file);
                $filename = str_replace("{$dir}/",'',$file);
                $res = DB::table($this->table)->insert(['path'=>$file,'file_size'=>$size,'filename'=>$filename]);
                $count += $res ? 1 : 0;
            }       
        }
        return $count;
    }

}
